package receiver.screens;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.impl.client.DefaultHttpClient;

import edu.washington.cs.sounddetector.server.api.SoundDetectorAPI;
import edu.washington.cs.sounddetector.server.api.object.Notification;
import edu.washington.cs.sounddetector.server.api.object.PeakEvent;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HistoryActivity extends Activity {
	private String phoneName;
	private String id;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.historyscreen);
		
		if(getIntent() != null && getIntent().getExtras() != null && getIntent().getExtras().getString("phoneName") != null && getIntent().getExtras().getString("id") != null) {
			phoneName = getIntent().getExtras().getString("phoneName");
			id = getIntent().getExtras().getString("id");
		}else {
			phoneName = "test";
			id = null;
		}
		LinearLayout layout = (LinearLayout) findViewById(R.id.history_layout);
		
		SoundDetectorAPI api = null;
		try {
			api = new SoundDetectorAPI(new URI("http://kiska.cs.washington.edu:8080/sound-detector-test"), new DefaultHttpClient());
		} catch (URISyntaxException e) {
			// TODO ERROR RECOVERY
			Log.e("API", Log.getStackTraceString(e));
		}
		List<Notification> ln = null;
		try {
			ln = api.getOldNotifications(phoneName, new Date(0), new Date());
		} catch (Exception e) {
			// TODO ERROR RECOVERY
			Log.e("BOOM", Log.getStackTraceString(e));
		}
		if(ln == null || ln.isEmpty())
			return;
		for(Notification n : ln) {
			TextView tv = new TextView(this);
			tv.setText(n.getDetectorId() + ": " + n.getSoundClass().getSoundClass());
			tv.setTextColor(Color.WHITE);
			layout.addView(tv);	
		}
   
	}
	
//	 private void visualize(Notification n) {
//	    	List<VisualizationData> data = new ArrayList<VisualizationData>();
//			for(PeakEvent peak : n.getPeakEvents()) {
//				VisualizationData d1 = new VisualizationData();
//				d1.duration = (long) (peak.getDuration() * 1000 / VisualizationView.tickRatio);
//				d1.start = (long) peak.getTime() * 1000 / VisualizationView.tickRatio;
//				d1.loudness = Math.min(30, (int) peak.getMagnitude());
//				d1.color = n.getSoundClass().getRgbColor();
//				data.add(d1);
//			}
//	        vis = new VisualizationView(this, data);
//	        repeat.setOnClickListener(new RepeatButtonListener(vis));
//	        layout.addView(vis, 0);
//	        guess.setText(n.getSoundClass().getSoundClass());
//	        location.setText(n.getDetectorId());
//	    }
	 
//	 private class listener implements OnClickListener {
//		 private Context context;
//		 private Notification n;
//
//		public listener(Notification n, HistoryActivity historyActivity) {
//			this.context = historyActivity;
//			this.n = n;
//		}
//
//		public void onClick(View v) {
//			Intent i = new Intent(context, ReceiverPhoneActivity.class);
//			i.putex("notification", n);
//			startActivity(i);
//			
//		}
//		 
//	 }

}
